<?php

require_once 'common.php';
require_once 'ResultData.php';
require_once 'config.php';
require_once 'Login.php';

session_start();


if (isGet()) {
    echo "不得以GET访问";
    exit;
}


$name = $_SESSION['username'];
$auto = $_SESSION['auto'];
$psd = trim($_SESSION['password']);


$result = new ResultData();

if ($auto == "") {
    $result->code = 404;
    $result->msg = "未登录，请先登录";
    exit(json_encode($result));
}

if ($auto == "0") {
    $result->msg = "登录成功";
    exit(json_encode($result));
}

$con = mysqli_connect($servername, $username, $password, $database, $port);

if (!$con) {
    $result->code = 404;
    $result->msg = "数据库连接不上";
    exit(json_encode($result));
}

$sql = "SELECT count(*) num FROM t_login WHERE name= '{$name}' AND password = '{$psd}'";


$record = $con->query($sql);

if ($record->num_rows > 0) {
    while ($row = $record->fetch_assoc()) {
        $result->msg = "登录成功";

        if ($row["num"] <= 0) {
            $result->code = 404;
            $result->msg = "用户名或密码不正确";
        } else {
            $sql = "SELECT id, name, password FROM t_login WHERE  name= '{$name}' AND password = '{$psd}'";

            $record2 = $con->query($sql);

            if ($record2->num_rows > 0) {
                $login = new Login();
                $result->data = $login;
                while ($row1 = $record2->fetch_assoc()) {
                    $login->id = $row1["id"];
                    $login->name = $row1["name"];
                    $login->password = $row1["password"];
                }
            }
        }
    }
}

mysqli_close($con);


exit(json_encode($result));
